package com.lancoo.znbkxx.teacher.viewmodel

import android.app.Application
import androidx.lifecycle.MutableLiveData
import com.lancoo.znbkxx.teacher.Ext.Companion.executeResponse
import com.lancoo.znbkxx.teacher.base.BaseViewModel
import com.lancoo.znbkxx.teacher.model.data.*
import com.lancoo.znbkxx.teacher.model.repository.PaperRepository

/**
 * @author : Admin
 * @date   : 2019/7/23 9:18
 * @desc   :
 */
class PaperViewModel(application: Application) : BaseViewModel(application) {
    val answerTime = MutableLiveData<String>()
    val startTime = MutableLiveData<String>()
    val endTime = MutableLiveData<String>()
    val searchInfo = MutableLiveData<String>()
    val pageSize = MutableLiveData<Int>()
    val pageNo = MutableLiveData<Int>()
    val mPaperList = MutableLiveData<PaperList>()
    val mSearchPaperList = MutableLiveData<SearchPaperList>()
    val mAnalysisInfo = MutableLiveData<AnalysisInfo>()
    val mEditDurationInfo = MutableLiveData<Boolean>()
    val mSubmittedInfo = MutableLiveData<MutableList<StuProgressInfo>>()
    val mClassFlag = MutableLiveData<MutableList<ClassFlag>>()
    val mPreviewStuHomeworkInfo = MutableLiveData<PreviewStuHomeworkInfo>()
    val mNewTask = MutableLiveData<MutableList<NewTask>>()
    val mPaperBaseInfoList = MutableLiveData<PaperBaseInfoList>()
    val mTeacherInfo = MutableLiveData<TeacherInfo>()
    val mHomeWorkAnalysis = MutableLiveData<MutableList<HomeWorkAnalysis>>()
    val mRZInfo = MutableLiveData<RZInfo>()
    val mErrorMsg = MutableLiveData<String>()
    val repository by lazy { PaperRepository() }
    val success = MutableLiveData<ReleaseInfo>()
    val mHastenCall = MutableLiveData<Int>()
    val mEffectiveInfo = MutableLiveData<PaperEffectiveInfo>()
    val mProgressValue = MutableLiveData<Float>()
    val mPaperTrailInfo = MutableLiveData<PaperTrailInfo>()
    val paperLayerInfo = MutableLiveData<PaperLayerInfo>()
    val schoolConfig = MutableLiveData<MutableList<PaperCatlog>>()
    val paperLibrary = MutableLiveData<PaperLibrary>()
    val StuID = MutableLiveData<String>()

    fun getPaperForMobileSearch(UserID: String, Token: String, SchoolID: String,
                                CourseClassID: String, Layer: String, SearchInfo: String,
                                LowDateTime: Long,
                                HighDateTime: Long, State: String, PageNumber: Int, PageSize: Int) {
        launch {
            val result = repository.getPaperForMobileSearch(UserID, Token, SchoolID, CourseClassID,
                    Layer, SearchInfo, LowDateTime, HighDateTime, State, PageNumber, PageSize)
            executeResponse(result, { mSearchPaperList.value = result.Data },
                    { mErrorMsg.value = result.Message })
        }
    }

    fun getPaperSetListForMobile(UserID: String, Token: String, SchoolID: String,
                                 CourseClassID: String, Layer: Int, PageNumber: Int,
                                 PageSize: Int) {
        launch {
            val result = repository.getPaperSetListForMobile(UserID, Token, SchoolID, CourseClassID,
                    Layer, PageNumber, PageSize)
            executeResponse(result, { mPaperList.value = result.Data },
                    { mErrorMsg.value = result.Message })
        }
    }

    fun getPaperSetForMobileTerminal(Key: String, SubjectID: String, GlobalGrade: String,
                                     CourseClassID: String, TID: String, SchoolID: String,
                                     Layer: Int, SearchInfo: String,
                                     State: String, DateField: String, PageNumber: Int,
                                     PageSize: Int) {
        launch {
            val result = repository.getPaperSetForMobileTerminal(Key, SubjectID, GlobalGrade,
                    CourseClassID, TID, SchoolID, Layer, SearchInfo, State, DateField, PageNumber,
                    PageSize)
            executeResponse(result, { mPaperList.value = result.Data }, { mErrorMsg.value = "" })
        }
    }

    fun getPaperForMobileSearch(Key: String, SubjectID: String, GlobalGrade: String,
                                CourseClassID: String, TID: String, SchoolID: String, Layer: String,
                                SearchInfo: String,
                                State: String, DateField: String, PageNumber: Int, PageSize: Int) {
        launch {
            val result = repository.getPaperForMobileSearch(Key, SubjectID, GlobalGrade,
                    CourseClassID, TID, SchoolID, Layer, SearchInfo, State, DateField, PageNumber,
                    PageSize)
            executeResponse(result, { mSearchPaperList.value = result.Data },
                    { mErrorMsg.value = "" })
        }
    }

    fun releaseAuditPaper(SetID: String, GlobalGrade: String, Token: String,
                          PaperList: MutableList<String>, StartTime: String, EndTime: String,
                          ClassList: MutableList<ClassFlag>, UserID: String, SchoolID: String,
                          Layer: Int,
                          PaperLibraryType: Int, TeacherName: String) {
        launch {
            val result = repository.releaseAuditPaper(SetID, GlobalGrade, Token, PaperList,
                    StartTime, EndTime, ClassList, UserID, SchoolID, Layer, PaperLibraryType,
                    TeacherName)
            executeResponse(result, { success.value = result.Data },
                    { mErrorMsg.value = result.Message })
        }
    }

    fun getDataAnalyze(CourseClassID: String, Key: String, SchoolID: String, token: String,
                       userID: String) {
        launch {
            val result = repository.getDataAnalyze(CourseClassID, Key, SchoolID, token, userID)
            executeResponse(result, { mAnalysisInfo.value = result.Data },
                    { mErrorMsg.value = result.Message })
        }
    }

    fun setPaperAnswerDuration(PaperID: String, CourseClassID: String, StartTime: String,
                               EndTime: String, Token: String, SchoolID: String, SetID: String,
                               GlobalGrade: String,
                               UserID: String) {
        launch {
            val result = repository.setPaperAnswerDuration(PaperID, CourseClassID, StartTime,
                    EndTime, Token, SchoolID, SetID, GlobalGrade, UserID)
            executeResponse(result, { mEditDurationInfo.value = result.Data },
                    { mErrorMsg.value = result.Message })
        }
    }

    fun getStuProgress(PaperID: String, CourseClassID: String, SchoolID: String, Token: String,
                       UserID: String) {
        launch {
            val result = repository.getStuProgress(PaperID, CourseClassID, SchoolID, Token, UserID)
            executeResponse(result, { mSubmittedInfo.value = result.Data },
                    { mErrorMsg.value = result.Message })
        }
    }

    fun getGradeClassWithPaperState(GlobalGrade: String, SchoolID: String, SetID: String,
                                    PaperID: String, Token: String, UserID: String) {
        launch {
            val result = repository.getGradeClassWithPaperState(GlobalGrade, SchoolID, SetID,
                    PaperID, Token, UserID)
            executeResponse(result, { mClassFlag.value = result.Data },
                    { mErrorMsg.value = result.Message })
        }
    }

    fun previewStuHomeworkInfo(Token: String, UserID: String, StuID: String, SchoolID: String,
                               Layer: Int) {
        launch {
            val result = repository.previewStuHomeworkInfo(Token, UserID, StuID, SchoolID, Layer)
            executeResponse(result, { mPreviewStuHomeworkInfo.value = result.Data },
                    { mErrorMsg.value = result.Message })
        }
    }

    fun getStuNewTask(Token: String, UserID: String, PageIndex: Int, PageSize: Int) {
        launch {
            val result = repository.getStuNewTask(Token, UserID, PageIndex, PageSize)
            executeResponse(result, { mNewTask.value = result.Data },
                    { mErrorMsg.value = result.Message })
        }
    }

    fun getPaperBaseInfoList(Token: String, UserID: String, StuID: String, SchoolID: String,
                             Layer: Int, PageIndex: Int, PageSize: Int, State: Int) {
        launch {
            val result = repository.getPaperBaseInfoList(Token, UserID, StuID, SchoolID, Layer,
                    PageIndex, PageSize, State)
            executeResponse(result, { mPaperBaseInfoList.value = result.Data },
                    { mErrorMsg.value = result.Message })
        }
    }

    fun getTeacherInfo(Token: String, UserID: String) {
        launch {
            val result = repository.getTeacherInfo(Token, UserID)
            executeResponse(result, { mTeacherInfo.value = result.Data },
                    { mErrorMsg.value = result.Message })
        }
    }

    fun getHomeworkStatisticAnalysis(Token: String, UserID: String, StuID: String,
                                     SchoolID: String) {
        launch {
            val result = repository.getHomeworkStatisticAnalysis(Token, UserID, StuID, SchoolID)
            executeResponse(result, { mHomeWorkAnalysis.value = result.Data },
                    { mErrorMsg.value = result.Message })
        }
    }

    fun getStuRZInfo(Token: String, UserID: String, StuID: String, SchoolID: String) {
        launch {
            val result = repository.getStuRZInfo(Token, UserID, StuID, SchoolID)
            executeResponse(result, { mRZInfo.value = result.Data },
                    { mErrorMsg.value = result.Message })
        }
    }

    fun doHastenCallOneOnOne(Token: String, UserID: String, StuID: String, PaperID: String,
                             SchoolID: String) {
        launch {
            val result = repository.doHastenCallOneOnOne(Token, UserID, StuID, PaperID, SchoolID)
            executeResponse(result, { mHastenCall.value = result.Data },
                    { mErrorMsg.value = result.Message })
        }
    }

    fun doHastenCall(Token: String, UserID: String, PaperID: String, SetID: String,
                     CourseClassID: String, PaperName: String, SchoolID: String) {
        launch {
            val result = repository.doHastenCall(Token, UserID, PaperID, SetID, CourseClassID,
                    PaperName,
                    SchoolID)
            executeResponse(result, { mHastenCall.value = result.Data },
                    { mErrorMsg.value = result.Message })
        }
    }

    fun getMobilePaperList(CourseClassID: String, IsRelease: Int, IsCompleted: Int, Layer: Int,
                           PageNumber: Int, PageSize: Int, SchoolID: String, Token: String,
                           UserID: String, SearchInfo: String) {
        launch {
            val result = repository.getMobilePaperList(CourseClassID, IsRelease, IsCompleted, Layer,
                    PageNumber, PageSize, SchoolID, Token, UserID, SearchInfo)
            executeResponse(result, { mPaperTrailInfo.value = result.Data },
                    { mErrorMsg.value = result.Message })
        }
    }

    fun getEffectivenessEvaluation(Token: String, UserID: String, CourseClassID: String) {
        launch {
            val result = repository.getEffectivenessEvaluation(Token, UserID, CourseClassID)
            executeResponse(result, {
                mEffectiveInfo.value = result.Data
                mProgressValue.value = if (result.Data.Effective.isEmpty()) 0f else result.Data.Effective.toFloat()
            }, { mErrorMsg.value = result.Message })
        }
    }

    fun getCatalog(token: String, userID: String, schoolID: String) {
        launch {
            val result = repository.getCatalog(token, userID, schoolID)
            executeResponse(result, { schoolConfig.value = result.Data }, {
                mErrorMsg.value = result.Message
            })
        }
    }

    fun getPaperList(PageIndex: Int, PageSize: Int, PaperType: String, SearchText: String) {
        launch {
            val result = repository.getPaperList(PageIndex, PageSize, PaperType, SearchText)
            executeResponse(result, { paperLibrary.value = result.Data }, {
                mErrorMsg.value = result.Message
            })
        }
    }

    fun getClassLayerPaperCount(Layer: Int) {
        launch {
            val result = repository.getClassLayerPaperCount(Layer)
            executeResponse(result, { paperLayerInfo.value = result.Data }, {
                mErrorMsg.value = result.Message
            })
        }
    }
}